<template>
	<view class="topic_single_practice_layout">
		<z-paging-swiper>
			<swiper :current="current">
				<swiper-item v-for="(item, index) in data" :key="item._id">
					<scroll-view scroll-y="true">
						<view class="container">
							<view class="container_tips flex-row">
								{{index + 1}}/{{data.length}}
								<text class="container_type flex-1"> [{{stringFromValue(item.topic.question_type)}}]
								</text>
								<view class="star-container" @click="favorite_click(item)">
									<uni-icons v-if="item.topic.is_like" type="star-filled" disa size="28"
										color="#dd524d"></uni-icons>
									<uni-icons v-else type="star" size="28" color="#222222"></uni-icons>
								</view>
							</view>
						</view>
						<topic-practice-item :item="item.topic"></topic-practice-item>
					</scroll-view>
				</swiper-item>
			</swiper>
		</z-paging-swiper>
	</view>
</template>

<script setup>
	import {
		onLoad
	} from '@dcloudio/uni-app'

	import {
		ref
	} from 'vue';

	import {
		stringFromValue
	} from '@/utils/question.js';

	const data = ref(null)
	const current = ref(null)
	const favorite = uniCloud.importObject('favoriteObj')
	const topic_problem = uniCloud.importObject('topic-problem')
	const opendb_note = uniCloud.importObject('opendb-note')

	onLoad(async (params) => {
		let res
		if (params.type == 0) {
			res = await topic_problem.topic_problem_all()
		} else if (params.type == 1) {
			res = await favorite.favorite_topic_all()
		} else if (params.type == 2) {
			res = await opendb_note.opendb_note_all()
		}
		console.log("所有题目详情", res);
		if (res.errCode != 0) {
			uni.showToast({
				title: res.errMsg,
				icon: 'fail'
			})
			return
		}
		data.value = res.data
		// 
		data.value.forEach(item => {
			if (item.topic.note) {
				item.topic.note.disabled = true
			}
		})
		current.value = data.value.findIndex(item => {
			return item.topic._id == params.id
		})
	})

	async function favorite_click(params) {
		let res = await favorite.favorite(params.topic._id, 2, params.topic.is_like)
		if (res.errCode != 0) {
			return
		}
		params.topic.is_like = !params.topic.is_like
		uni.showToast({
			title: params.topic.is_like ? '收藏成功' : '取消收藏成功',
			icon: 'success'
		})
		uni.$emit('topic_favorite')
	}
</script>

<style lang="scss" scoped>
	.topic_single_practice_layout {
		swiper {
			height: 100%;

			scroll-view {
				height: 100%;
			}
		}

		.container {
			padding: 0 40rpx;

			.container_tips {
				align-items: center;
				font-size: 32rpx;
				margin-top: 24rpx;
				color: $me-color-gray-1;

				.container_type {
					margin-left: 8rpx;
					font-size: 32rpx;
					color: $me-color-blue;
				}

				.star-container {
					padding: 0 10rpx;
					justify-content: flex-end;
				}
			}
		}
	}
</style>